#!/bin/python

# --------------------------------------
# VIDEO BUDDHA 2 - main window
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  definitions.db and based on a specific list of file 
#  extensions.
# --------------------------------------

import ConfigParser
import os
import sys
import wx

import logger.logger
import renamer.renamer

import views.win_about
import views.win_edit_options
import views.win_edit_defs

class WinMain(wx.Frame):
	
	def __init__(self):
		wx.Frame.__init__(self, None, wx.ID_ANY, u"Video Buddha", wx.DefaultPosition, wx.Size(600, 300), wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
		
		# Config
		self.config = ConfigParser.RawConfigParser()
		self.config.read('config.cfg')		
		
		# Class declarations
		self.rnm = renamer.renamer.Renamer()
		self.logger = logger.logger.Logger()
		
		# File infos
		self.file_infos = []
		
		# Application icon
		favicon = wx.Icon('res/favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
		self.SetIcon(favicon)
		
		self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
		
		sizer_top = wx.BoxSizer(wx.VERTICAL)
		
		self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
		sizer_main = wx.BoxSizer(wx.VERTICAL)
		
		# List control
		self.list_ctrl = wx.ListCtrl(self.panel, size = (-1, -1), style = wx.LC_REPORT | wx.BORDER_SUNKEN)
		sizer_main.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
		self.list_ctrl.InsertColumn(1, 'File', wx.LIST_AUTOSIZE_USEHEADER, 270)
		self.list_ctrl.InsertColumn(2, 'Rename To', wx.LIST_AUTOSIZE_USEHEADER, 275)
		
		# Browse sizer
		sizer_browse = wx.BoxSizer(wx.HORIZONTAL)
		sizer_main.Add(sizer_browse, 0, wx.ALL | wx.EXPAND, 0)
		
		# Directory chooser
		self.dir_picker = wx.DirPickerCtrl(self.panel, wx.ID_ANY, wx.EmptyString, u"Select a folder", wx.DefaultPosition, wx.DefaultSize, wx.DIRP_DEFAULT_STYLE)
		button_rename = wx.Button(self.panel, wx.ID_ANY, u"Rename Files", size = (100, -1))
		sizer_browse.Add(self.dir_picker, 1, wx.ALL | wx.EXPAND, 5)
		sizer_browse.AddSpacer((10, 0), 0, wx.EXPAND, 5)
		sizer_browse.Add(button_rename, 0, wx.ALL | wx.EXPAND, 5)
		
		self.panel.SetSizer(sizer_main)
		self.panel.Layout()
		sizer_main.Fit(self.panel)
		sizer_top.Add(self.panel, 1, wx.EXPAND |wx.ALL, 0)
		
		self.SetSizer(sizer_top)
		self.Layout()
		self.status_bar = self.CreateStatusBar(1, wx.ST_SIZEGRIP, wx.ID_ANY)
		
		# Menu bar
		self.menubar = wx.MenuBar(0)
		
		# File menu
		self.menu_file = wx.Menu()
		self.menubar.Append(self.menu_file, u"File" )
		
		# File -> Browse
		self.menu_file_browse = wx.MenuItem(self.menu_file, wx.ID_ANY, u"Browse", u"Browse for your video files", wx.ITEM_NORMAL )
		self.menu_file.AppendItem(self.menu_file_browse)
		
		# File -> Refresh
		self.menuItemRefresh = wx.MenuItem(self.menu_file, wx.ID_ANY, u"Refresh Listing", u"Refresh the file listing", wx.ITEM_NORMAL )
		self.menu_file.AppendItem(self.menuItemRefresh)
		
		self.menu_file.AppendSeparator()
		
		# File -> Exit
		self.menu_file_exit = wx.MenuItem(self.menu_file, wx.ID_ANY, u"Exit", u"Exit the program", wx.ITEM_NORMAL)
		self.menu_file.AppendItem(self.menu_file_exit)
		
		# Edit menu
		self.menu_edit = wx.Menu()
		self.menubar.Append(self.menu_edit, u"Edit")
		
		# Edit -> Definitions
		self.menu_edit_defs = wx.MenuItem(self.menu_edit, wx.ID_ANY, u"Definitions", u"Edit definitions for renaming files", wx.ITEM_NORMAL)
		self.menu_edit.AppendItem(self.menu_edit_defs)
		
		# Edit -> Options
		self.menu_edit_options = wx.MenuItem(self.menu_edit, wx.ID_ANY, u"Options", u"Edit configuration options", wx.ITEM_NORMAL)
		self.menu_edit.AppendItem(self.menu_edit_options)
		
		# Help menu
		self.menu_help = wx.Menu()
		self.menubar.Append(self.menu_help, u"Help")
		
		# Help -> About
		self.menu_help_about = wx.MenuItem(self.menu_help, wx.ID_ANY, u"About", u"About Video Renamer", wx.ITEM_NORMAL)
		self.menu_help.AppendItem(self.menu_help_about)
		
		self.SetMenuBar(self.menubar)
		
		self.Centre(wx.BOTH)
		
		# Connect Events
		self.Bind(wx.EVT_DIRPICKER_CHANGED, self.on_dir_changed, self.dir_picker)
		self.Bind(wx.EVT_MENU, self.on_browse, self.menu_file_browse)
		self.Bind(wx.EVT_MENU, self.OnExit, self.menu_file_exit)
		self.Bind(wx.EVT_MENU, self.on_edit_defs, self.menu_edit_defs)
		self.Bind(wx.EVT_MENU, self.on_edit_options, self.menu_edit_options)
		self.Bind(wx.EVT_MENU, self.on_about, self.menu_help_about)
		self.Bind(wx.EVT_MENU, self.on_refresh, self.menuItemRefresh)
		self.Bind(wx.EVT_BUTTON, self.on_rename, button_rename)
		self.Bind(wx.EVT_CLOSE, self.OnExit)
		
		# Set the default directory if there is one saved
		dir = self.rnm.config.get('main', 'saved_dir')
		self.rnm.set_dir(dir)
		
		self.dir_picker.Path = dir
		
		self.update_list_ctrl()
	
	def __del__(self):
		self.Destroy()
		sys.exit(0)

	def OnExit(self, event):
		self.Destroy()
		sys.exit(0)
		
	def on_refresh(self, event):
		self.rnm.set_dir(self.dir_picker.Path)
		
		self.update_list_ctrl()
	
	def on_dir_changed(self, event):
		# Update the directory listing
		if self.rnm.set_dir(self.dir_picker.Path):
			self.update_list_ctrl()
			
			# Update the config file if wanted
			if self.config.getboolean('main', 'b_remember_dir'):	
				cfg = open('config.cfg', 'w')
				self.config.set('main', 'saved_dir', self.dir_picker.GetPath())
				self.config.write(cfg)
				cfg.close()			
		# Note: if the directory doesn't exist, this event will never be fired, the
		# set_dir function also checks if it exists, so it's a bit redundent
			
	def update_list_ctrl(self):
		# Delete the old items
		self.list_ctrl.DeleteAllItems()
		
		# Get the files		
		self.file_infos = self.rnm.get_complete_file_infos()
		
		index = 0
		
		# Run through ALL the files in the directory
		for file_info in self.file_infos:
			self.list_ctrl.InsertStringItem(index, file_info.file_name + file_info.extension)
			
			# Only do shit to files that have the appropriate info needed to be renamed
			if file_info.show_name is not None:
					self.list_ctrl.SetStringItem(index, 1, file_info.new_name + file_info.extension)

			# Alt row colors
			if index % 2:
				self.list_ctrl.SetItemBackgroundColour(index, wx.Colour(224, 224, 255))	
			
			index += 1
		
		self.status_bar.SetStatusText(str(index) + " video(s) have been found.")
	
	def on_rename(self, event):
		renamed_count = self.rnm.rename(self.file_infos)
		
		if renamed_count > 0:
			self.status_bar.SetStatusText(str(renamed_count) + " video(s) have been renamed.")
		else:
			self.status_bar.SetStatusText("No videos have been renamed.")
	
	def on_browse(self, event):
		event.Skip()
	
	def on_edit_defs(self, event):
		window_edit_defs = views.win_edit_defs.WinEditDefs()
		window_edit_defs.Show()
		
	def on_edit_options(self, event):
		window_edit_options = views.win_edit_options.WinEditOptions()
		window_edit_options.Show()
	
	def on_about(self, event):	
		window_about = views.win_about.WinAbout()
		window_about.Show()